<?php
namespace app\admin\controller;

class Content extends Admin{
    public function __construct(){
    	parent::__construct();
        $this->model = 'Content';
        $this->assign("model",$this->model);
    }
    // public function index(){
    //     $getData = input('param.');
    //     $whereData = '';
    //     if ($this->uinfo['roleid'] != 1) {
    //         $whereData = 'userid='.$this->uinfo['userid'];
    //     }
    //     if ($getData) {
    //         foreach ($getData as $k=>$v) {
    //             if (!$whereData) {
    //                 $whereData .= $k.'='.$v;
    //             }else{
    //                 $whereData .= '&'.$k.'='.$v;
    //             }
    //         }
    //     }
    //     $this->assign('keywords', isset($getData['keywords']) ? $getData['keywords'] : '');
    //     $this->assign("whereData",$whereData);
    //     return $this->fetch();
    // }
    protected function get_where_map(){
        $keywords = input('param.keywords');
        $whereDatas = [];
        if ($this->uinfo['roleid'] != 1) {
            $whereDatas['userid'] = $this->uinfo['userid'];
        }
        if ($keywords) {
            $whereDatas['id|title|descrip'] = ['like', "%$keywords%"];
        }
        return $whereDatas;
    }
    public function data_save(){
        $db = model($this->model);
        $postData = input("post.postData");
        parse_str($postData,$dataarr);
        $dataarr['userid'] = $this->uinfo['userid'];
        $dataarr['roleid'] = $this->uinfo['roleid'];
        $id = $db->save_data($dataarr);
        if($id === false){
            return json(['status' => 0,'msg'=>$db->getError()]);
        }
        if ($this->uinfo['roleid'] == 6) {
            $r = [
                'contentid' => $id,
                'typeid' => 1,
                'dealer_id' => $this->uinfo['userid'],
                'roleid' => $this->uinfo['roleid']
            ];
            db('DealerContent')->insert($r);
        }
        return json(['status' => 1,'msg'=>'操作成功']);
    }
    public function add(){
    	$menus_list = model("Category")->order("id")->select();
    	$menus_list = json_decode(json_encode($menus_list),true);
    	$Tree = new \base\tree\Tree;
        $menus = $Tree->toFormatTree($menus_list);
        $menus = array_merge([0 => ['id'=>0,'title_show'=>'顶级分类']], $menus);
        $this->assign('Menus', $menus);
    	return $this->fetch();
    }
    public function edit(){
    	$items = input("param.items");
        $db = model($this->model);
        $info = $db->get_data(['item_id' => $items]);
        $this->assign("info",$info);
    	
    	$menus_list = model("Category")->order("id")->select();
    	$menus_list = json_decode(json_encode($menus_list),true);
    	$Tree = new \base\tree\Tree;
        $menus = $Tree->toFormatTree($menus_list);
        $menus = array_merge([0 => ['id'=>0,'title_show'=>'顶级分类']], $menus);
        $this->assign('Menus', $menus);
    	return $this->fetch();
    }
    public function sel_dealer(){
        $id = input('param.id');
        $dealer_list = db('DealerContent')->where('contentid', $id)->where('typeid', 1)->select();
        $dealer_ids = [];
        foreach ($dealer_list as $v) {
            $dealer_ids[] = $v['dealer_id'];
        }
        $list = model('AdminUser')->list_data(['puserid'=>$this->uinfo['userid'], 'roleid'=>6]);
        $this->assign('list', $list);
        $this->assign('dealer_list', $dealer_ids);
        $this->assign('id', $id);
        return $this->fetch();
    }
    public function set_dealer(){
        $db = model($this->model);
        $postData = input("post.postData");
        parse_str($postData,$dataarr);
        db('DealerContent')->where('contentid', $dataarr['id'])->where('typeid', 1)->delete();
        foreach ($dataarr['dealer_ids'] as $v) {
            $r = [
                'contentid' => $dataarr['id'],
                'typeid' => 1,
                'dealer_id' => $v,
                'roleid' => $this->uinfo['roleid']
            ];
            db('DealerContent')->insert($r);
        }
        // $dealer_ids = implode(',', $dataarr['dealer_ids']);
        // $ids = $db->where('id', $dataarr['id'])->setField('dealer_ids', $dealer_ids);
        if($ids === false){
            return json(['status' => 0,'msg'=>$db->getError()]);
        }
        return json(['status' => 1,'msg'=>'操作成功']);
    }
}